class Tree {
    constructor() {
        this.isDictionary = false;  //默认不是字典树
        this.root = {};     //树的根节点 整棵树的结构
    }

    //遍历 查询是否存在该元素  返回true 存在 false不存在
    select(object,current,total,root) {
        if(!total) {
            total = 0;
            for(let key in object) {
                total++;
            }
        }
        root || (root = this.root);

        current || (current = 0);
        current++;
        let now = 0;
        for(let key in object) {
            now ++;
            if(current === now) {
                if(!root[object[key]]) {
                    return false;
                }
    
                if(root[object[key]] && (current === total)) {
                    return true;
                } else if(root[object[key]] && (current !== total)) {
                    //继续递归
                    return this.select(object,current,total,root[object[key]]);
                }
            } else {
                continue;
            }
            
        }
    }

    //插入 如果存在返回false，插入成功返回true
    insert(object,current,total,root) {
        root || (root = this.root);
        //先统计对象的key数
        if(!total) {
            total = 0;
            for(let key in object) {
                total++;
            }
        }
        //计算当前遍历的对象是不是需要遍历的
        current || (current = 0);
        let now = 0;
        current++;


        //不存在就插入 一直到对象遍历结束
        for(let key in object) {
            now++;
            if(now === current) {
                if(!root[object[key]] && (current !== total)) {
                    root[object[key]] = {};
                    return this.insert(object,current,total,root[object[key]]);
                } else if (!root[object[key]] && (current === total)) {
                    root[object[key]] = {};
                    return true;
                }
                //存在且完全符合则返回false
                if(root[object[key]] && (current === total)) {
                    return false;
                } else if(root[object[key]] && (current !== total)) {
                    //继续递归
                    return this.insert(object,current,total,root[object[key]]);
                }
            } else {
                continue;
            }
            
        }
    }

    //生长 遍历是否有这个值 没有就插入 返回true，有的话会返回false
    grow(object,current,total,root) {
        root || (root = this.root);
        //先统计对象的key数
        if(!total) {
            total = 0;
            for(let key in object) {
                total++;
            }
        }
        //计算当前遍历的对象是不是需要遍历的
        current || (current = 0);
        let now = 0;
        current++;


        //不存在就插入 一直到对象遍历结束
        for(let key in object) {
            now++;
            if(now === current) {
                if(!root[object[key]] && (current !== total)) {
                    root[object[key]] = {};
                    return this.grow(object,current,total,root[object[key]]);
                } else if (!root[object[key]] && (current === total)) {
                    root[object[key]] = {};
                    return true;
                }
                //存在且完全符合则返回false
                if(root[object[key]] && (current === total)) {
                    return false;
                } else if(root[object[key]] && (current !== total)) {
                    //继续递归
                    return this.grow(object,current,total,root[object[key]]);
                }
            } else {
                continue;
            }
            
        }
    }


    //删除某节点及接下来的所有元素
    delete(object,current,total,root) {
        root || (root = this.root);
        //先统计对象的key数
        if(!total) {
            total = 0;
            for(let key in object) {
                total++;
            }
        }
        //计算当前遍历的对象是不是需要遍历的
        current || (current = 0);
        let now = 0;
        current++;
        //删除是完全相同再删除
        for(let key in object) {
            now++;
            if(now === current) {
                if(!root[object[key]]) {
                    return false;
                } 
                if(root[object[key]] && (current === total)) {
                    delete root[object[key]];
                    return true;
                } else if(root[object[key]] && (current !== total)) {
                    //继续递归
                    return this.delete(object,current,total,root[object[key]]);
                }
            } else {
                continue;
            }
            
        }
    }

    //清空树
    clear() {
        this.root = {};
    }

    walk() {
        return this.root;
    }
    
}

module.exports = Tree;